import 'package:dmz/base/pageWidget/base_stateful_widget.dart';
import 'package:dmz/base/widget/defult_widget.dart';
import 'package:dmz/business/home/controller/home_main_controller.dart';
import 'package:dmz/business/home/model/moto_model.dart';
import 'package:dmz/string_res/str_res_keys.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class HomeWidgets {
  static Widget clickModelWidget(Function(HomeMotoAction type) onTap,
      {MotoModel? model}) {
    return Row(mainAxisAlignment: MainAxisAlignment.center, children: [
      Image.asset(
              model != null && model.sateOpen
                  ? 'assets/images/home/main/hc_icon_open.png'
                  : 'assets/images/home/main/hc_icon.png',
              width: 128.sp,
              height: 128.sp)
          .addGestureDetector(() =>
              onTap(model != null ? HomeMotoAction.seat : HomeMotoAction.none))
          .marginOnly(right: 60.sp),
      Image.asset(
        model != null && model.isOpen
            ? 'assets/images/home/main/power_supply_select.png'
            : 'assets/images/home/main/power_supply.png',
        width: 192.sp,
        height: 192.sp,
      ).addGestureDetector(() =>
          onTap(model != null ? HomeMotoAction.open : HomeMotoAction.none)),
      Image.asset('assets/images/home/main/dw_icon.png',
              width: 128.sp,
              height: 128.sp,
              color: model != null && model.funded ? Colors.green : null)
          .addGestureDetector(() => onTap(
              model != null ? HomeMotoAction.foundDevice : HomeMotoAction.none))
          .marginOnly(left: 60.sp)
    ]);
  }

  static Widget topWidget(
      BuildContext context, Function(HomeMotoAction type) clickAction,
      {MotoModel? data}) {
    return Stack(children: [
      Container(
          alignment: Alignment.center,
          height: 66.sp,
          child: DefaultText(data?.name ?? 'Smart bike V50', 48.sp,
              textColor: Colors.white)),
      if (data == null)
        Positioned(
            right: 10.sp,
            child: Container(
                    alignment: Alignment.center,
                    width: 88.sp,
                    height: 58.sp,
                    child: Image.asset('assets/images/home/main/more_icon.png',
                        width: 38.sp, height: 10.sp))
                .addGestureDetector(() => clickAction(HomeMotoAction.set)))
    ]);
  }

  static Widget positionWidget(Function(HomeMotoAction type) clickAction,
      {MotoModel? data}) {
    return Stack(alignment: Alignment.center, children: [
      Container(
        alignment: Alignment.topCenter,
        color: Colors.transparent,
        padding: EdgeInsets.only(bottom: data == null ? 50.sp : 0),
        child: data == null
            ? Image.asset('assets/images/home/main/back_icon.png',
                height: 500.sp, width: 790.sp)
            : Container(
                color: Colors.transparent,
                child: DefaultNetWorkImage(
                  data.photo ?? '',
                  otherWidget: Image.asset(
                          'assets/images/home/main/back_icon.png',
                          height: 500.sp,
                          width: 790.sp)
                      .marginOnly(
                          top: 200
                              .sp), /*otherWidth: 568.sp, otherHight: 500.sp*/
                )),
      ),
      if (data == null)
        Positioned(
            bottom: 0,
            child: Image.asset('assets/images/home/main/add_mobil.png',
                    width: 172.sp, height: 172.sp)
                .addGestureDetector(() => clickAction(HomeMotoAction.add)))
    ]).marginOnly(top: data == null ? 132.sp : 92.sp);
  }

  static Widget operateWidget(
      context, Function(HomeMotoAction type, {int? other}) clickAction,
      {bool isMoto = false, MotoModel? model}) {
    return !isMoto
        ? HomeWidgets.clickModelWidget(clickAction)
            .marginOnly(bottom: isMoto ? 0.sp : 386.sp)
        : Stack(alignment: Alignment.bottomCenter, children: [
            Container(
                color: Colors.transparent,
                padding: EdgeInsets.only(bottom: 455.sp),
                child: HomeWidgets.clickModelWidget(clickAction, model: model)),
            Column(children: [
              vehicleStatusWidgets(context, model),
              actionItems(clickAction, model!)
            ])
          ]).marginOnly(bottom: MediaQuery.of(context).padding.bottom + 50.sp);
  }

  static Widget vehicleStatusWidgets(context, MotoModel? model) {
    items(bool isLeft, Widget child) {
      return Container(
          width: 372.sp,
          height: 250.sp,
          decoration: BoxDecoration(
              image: DecorationImage(
                  image: AssetImage(isLeft
                      ? 'assets/images/home/main/back_left.png'
                      : 'assets/images/home/main/back_right.png'),
                  fit: BoxFit.fill)),
          padding: EdgeInsets.only(top: 36.sp, bottom: 28.sp, left: 20.sp),
          child:
              Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
            Row(mainAxisAlignment: MainAxisAlignment.start, children: [
              Image.asset('assets/images/home/main/mileage_icon.png',
                      width: 74.sp, height: 74.sp)
                  .marginOnly(right: 26.sp),
              DefaultText(isLeft ? SR.lastMileage.tr : SR.power.tr, 32.sp,
                  textColor: Colors.white),
              if (!isLeft)
                DefaultText('${model?.power}%', 44.sp,
                        textColor: Colors.white, fontWeight: FontWeight.w700)
                    .marginOnly(left: 32.sp)
            ]).marginOnly(bottom: 30.sp),
            child
          ]));
    }

    return Row(mainAxisAlignment: MainAxisAlignment.spaceAround, children: [
      items(
          true,
          DefaultText('${500 * (model!.power) * 0.01} km', 44.sp,
              textColor: Colors.white, fontWeight: FontWeight.w700)),
      items(
          false,
          Image.asset('assets/images/home/main/prow_${model.power ~/ 20}.png',
              width: 334.sp, height: 80.sp)),
    ]).marginOnly(left: 32.sp, right: 32.sp, bottom: 20.sp);
  }

  static Widget actionItems(
      Function(HomeMotoAction type, {int? other}) click, MotoModel model) {
    item(int index) {
      return Column(children: [
        Image.asset('assets/images/home/main/bottom_$index.png',
            width: 112.sp, height: 112.sp),
        DefaultText(model.configStr![index].tr, 32.sp, textColor: Colors.white)
      ])
          .marginOnly(top: 20.sp)
          .addGestureDetector(() => click(HomeMotoAction.bottom, other: index));
    }

    return model.configResult == null || model.configResult!.isEmpty
        ? Container()
        : Container(
            alignment: Alignment.center,
            height: 204.sp,
            decoration: BoxDecoration(
              color: const Color(0xFF434650),
              borderRadius: BorderRadius.circular(28.sp), // 设置圆角半径
            ),
            margin: EdgeInsets.only(left: 32.sp, right: 32.sp),
            child: PageView(children: [
              ListView.separated(
                  padding: EdgeInsets.only(left: 28.sp),
                  scrollDirection: Axis.horizontal,
                  itemBuilder: (context, index) {
                    return item(model.configResult![index]);
                  },
                  separatorBuilder: (context, index) {
                    return SizedBox(width: 160.sp);
                  },
                  itemCount: 3),
              if (model.configResult!.length > 3)
                ListView.separated(
                    physics: const NeverScrollableScrollPhysics(),
                    padding: EdgeInsets.only(left: 28.sp),
                    scrollDirection: Axis.horizontal,
                    itemBuilder: (context, index) {
                      return index + 3 > model.configResult!.length - 1
                          ? const SizedBox()
                          : item(model.configResult![index + 3]);
                    },
                    separatorBuilder: (context, index) {
                      return SizedBox(width: 160.sp);
                    },
                    itemCount: 3),
            ]));
  }

  static Widget typeIcons(
      MotoModel data, Function(HomeMotoAction action) action) {
    return Container(
        color: Colors.transparent,
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            DefaultIconBtn(
                'assets/images/home/main/4g_icon_no.png', data.have4G,
                selectImage: 'assets/images/home/main/4g_icon.png',
                click: () => action(HomeMotoAction.other),
                width: 96.sp,
                height: 96.sp),
            DefaultIconBtn('assets/images/home/main/set_icon.png', false,
                selectImage: 'assets/images/home/main/set_icon.png',
                click: () => action(HomeMotoAction.set),
                width: 96.sp,
                height: 96.sp)
          ]).marginOnly(left: 20.sp, right: 20.sp),
          Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
            DefaultIconBtn(
                'assets/images/home/main/gts_icon_no.png', data.haveGPS,
                selectImage: 'assets/images/home/main/gts_icon.png',
                width: 96.sp,
                height: 96.sp),
            DefaultIconBtn(
                'assets/images/home/main/blue_icon_no.png', data.contentBlue,
                selectImage: 'assets/images/home/main/blue_icon.png',
                click: () => action(HomeMotoAction.contnent),
                width: 96.sp,
                height: 96.sp)
          ]).marginOnly(top: 48.sp, left: 20.sp, right: 20.sp),
        ]).marginOnly(top: 66.sp));
  }

  static Widget changeIcons(Function(HomeMotoAction action) action) {
    return Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [
      DefaultIconBtn('assets/images/home/main/next_right.png', false,
          click: () => action(HomeMotoAction.previous),
          width: 150.sp,
          height: 407.sp),
      DefaultIconBtn('assets/images/home/main/next_left.png', false,
          click: () => action(HomeMotoAction.netxt),
          width: 150.sp,
          height: 407.sp)
    ]).marginOnly(top: 312.sp + 300.sp);
  }

  static Widget shareWidget(Function() click) {
    return Positioned(
        top: 580.sp,
        child: Container(
            alignment: Alignment.center,
            color: const Color(0xFFF4FFF0),
            height: 92.sp,
            width: Get.width,
            child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [
              Image.asset('assets/images/home/share/share_new_icon.png',
                      width: 56.sp, height: 56.sp)
                  .marginOnly(right: 16.sp),
              DefaultText('你有待接收车辆分享的提醒，请查看', 32.sp,
                  textColor: const Color(0xFF000000))
            ])).addGestureDetector(click));
  }
}
